package icu.sehnen.user.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import icu.sehnen.common.result.CommonResult;
import icu.sehnen.user.entity.Role;
import icu.sehnen.user.service.IRolePermissionRelationService;
import icu.sehnen.user.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 后台用户角色和权限关系表 前端控制器
 * </p>
 *
 * @author icu.sehnen
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/user/role/permission")
public class RolePermissionRelationController {
    @Autowired
    IRolePermissionRelationService rolePermissionRelationService;
    @Autowired
    IRoleService roleService;
    @PostMapping("/delete")
    public CommonResult deletePermissionById(Long roleId, Long rightId ){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_id",roleId);
        queryWrapper.eq("permission_id",rightId);

       Boolean res =rolePermissionRelationService.remove(queryWrapper);
        Role byId = roleService.getRoleById(roleId);
        if(byId!=null&&res){
            return CommonResult.success(byId);
        }
        return  CommonResult.failed();
    }

    @PostMapping("/add/{id}")
    public CommonResult addRights(Long[] ids,@PathVariable Long id){
        long start = System.currentTimeMillis();
        System.out.println(ids.toString() + id);
       rolePermissionRelationService.addRight(ids,id);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
            return CommonResult.success();
    }

 }
